package org.helgoboss.scala_additions

class NestedMap[K](m: Map[K, Any]) {
    def getDeep(pathComponents: K*): Option[Any] = getDeep(m, pathComponents.iterator)
    
    private def getDeep(value: Any, keyIterator: Iterator[K]): Option[Any] = {
        value match {
            case m: Map[K, Any] =>
                /* The value is a map */
                if (keyIterator.hasNext) {
                    /* Keys to lookup left */
                    val key = keyIterator.next
                    m.get(key) match {
                        case Some(nextValue) => getDeep(nextValue, keyIterator)
                        case None => None
                    }
                } else {
                    /* No more keys to lookup left */
                    Some(m)
                }
            case _ =>
                /* The value is not a map */
                if (keyIterator.hasNext) {
                    /* Keys to lookup left */
                    None
                } else {
                    /* No more keys to lookup left */
                    Some(value)
                }
        }
    }
}